#!/usr/bin/env perl

use FindBin;
use lib ($FindBin::Bin);
use Pasa_init;
use DB_connect;
use strict;
use DBI;
use Storable qw(lock_retrieve);
use CDNA::CDNA_alignment;
use Getopt::Std;

our $SEE = 0;

use vars qw ($opt_h $opt_K $opt_D $opt_p $opt_d $DEBUG $opt_M $opt_O);

&getopts ('hD:dp:M:O:K');

open (STDERR, "&>STDOUT");

my $usage =  <<_EOH_;

Script loads a MySQL db with the assemblies in the /assembly directory.

############################# Options ###############################
#
# -M Mysql database/server ie. ("ath1_cdnas:haasbox")
# 
# -p password file  (contains "username:password")
# 
# -d Debug
# 
# -h print this option menu and quit
#
# -K keep loaded gene annotations
###################### Process Args and Options #####################

_EOH_

    ;

if ($opt_h) {die $usage;}
my $MYSQLstring = $opt_M or die $usage;
my ($MYSQLdb, $MYSQLserver) = split (/:/, $MYSQLstring); 
my $DEBUG = $opt_d;
my $passwordinfo = $opt_p or die $usage;
my ($user, $password) = split (/:/, $passwordinfo);


my ($dbproc) = &connect_to_db($MYSQLserver,$MYSQLdb,$user,$password);

my $query = "show tables";
my @results = &do_sql_2D($dbproc, $query);

my  %keep = (annotation_admin => 1,
	     annotation_compare => 1,
	     annotation_store => 1);

foreach my $result (@results) {
    my ($table) = @$result;
    
    if ($table eq "status") { next;} #never purge this table.

    if ($opt_K && $keep{$table}) { next;}
    

    print "Purging table: ${MYSQLdb}::${table}\n"; 
    my $query = "delete from $table";
    &RunMod($dbproc, $query);
}
print "\ndone.\n\n";

$dbproc->disconnect;

exit(0);

    
